﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LifeChess.Util
{
    public class ByteArrayStream
    {
        public List<byte> buffer;

        #region Method Write
        public void Write(bool value)
        {
            buffer.AddRange(BitConverter.GetBytes(value));
        }

        public void Write(int value)
        {
            buffer.AddRange(BitConverter.GetBytes(value));
        }

        public void Write(float value)
        {
            buffer.AddRange(BitConverter.GetBytes(value));
        }

        public void Write(char value)
        {
            buffer.AddRange(BitConverter.GetBytes(value));
        }

        public void Write(string value)
        {
            buffer.AddRange(Encoding.UTF8.GetBytes(value));
        }
        #endregion

        #region Method Read
        public bool ReadBool()
        {
            bool result = BitConverter.ToBoolean(ToArray(), 0);
            buffer.RemoveRange(0, sizeof(bool));
            return result;
        }
        public int ReadInt()
        {
            int result = BitConverter.ToInt32(ToArray(), 0);
            buffer.RemoveRange(0, sizeof(int));
            return result;
        }

        public float ReadFloat()
        {
            float result = BitConverter.ToSingle(ToArray(), 0);
            buffer.RemoveRange(0, sizeof(float));
            return result;
        }

        public char ReadChar()
        {
            char result = BitConverter.ToChar(ToArray(), 0);
            buffer.RemoveRange(0, sizeof(char));
            return result;
        }

        public string ReadString(int len)
        {
            string result = Encoding.UTF8.GetString(ToArray().Take(len).ToArray());
            buffer.RemoveRange(0, len * sizeof(char));
            return result;
        }
        #endregion
        public byte[] ToArray() => buffer.ToArray();
    }
}
